<?php


namespace app\service;


use app\dao\RolesDao;
use app\dao\UserDao;

class UserService
{
    private $UserDao;
    private $RolesDao;

    public function __construct()
    {
        $this->UserDao = new UserDao();
        $this->RolesDao = new RolesDao();
    }

    private function setPassword(String $password , String $salt){
        $password = md5(md5($password).md5($salt));
        return $password;
    }

    public function addHotelAdmin($name, $head_img){
        $password = rand(10000000 , 99999999);
        $salt = '';
        for($i = 0 ; $i < 8 ; $i++){
            $salt = $salt.chr(mt_rand(97, 122));
        }

        $user = $this->UserDao->save([
            'username' => $name,
            'head_img' => $head_img,
            'password' => $this->setPassword($password , $salt),
            'salt' => $salt,
            'role' => $this->RolesDao->customQuery()->filterByName('酒店管理员')->getOne()->id
        ]);

        $user['pwd'] = $password;
        return $user;
    }

}